//
//  XLTarBarController.swift
//  XLWibo-Swift
//
//  Created by 王林 on 2016/11/12.
//  Copyright © 2016年 木木. All rights reserved.
//

import UIKit

class WBTarBarController: UITabBarController {
   
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let XtabBar = WBtabBar()
        //        self.tabBar = tabBar
        setValue(XtabBar, forKey: "tabBar")
        XtabBar.clickAddBtn = {[weak self] in
            print("点击了加号")
             let  KKView:WBPresentView = WBPresentView()
//            UIApplication.shared.keyWindow?.addSubview(KKView)
            KKView.show(fromVC: self)
        }
        
        addChildViewControllerS()
        
    }

    private func addChildViewControllerS() {
        addChildViewController(vc: WBHomeViewController(), title: "首页", imageName: "tabbar_home",index:0)
        addChildViewController(vc: WBMessageViewController(), title: "消息", imageName: "tabbar_message_center",index:1)
        addChildViewController(vc: WBDiscoverViewController(), title: "发现", imageName: "tabbar_discover",index:2)
        addChildViewController(vc: WBProfileViewController(), title: "我的", imageName: "tabbar_profile",index:3)
        
    }
    
    private func addChildViewController(vc:UIViewController, title:String ,imageName:String,index:Int){
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        vc.tabBarItem.tag = index
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: .normal)
        
        let nav = WBNavigationController(rootViewController: vc)
        addChildViewController(nav)
        
    }
    
    
    //遍历
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        print(item.tag)
        var index = 0
        
        for subView  in tabBar.subviews {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                if  index == item.tag{
                    //遍历barButton的子视图
                    for V in subView.subviews {
                        if  V.isKind(of: NSClassFromString("UIImageView")!){
                            print("做动画")
                            //先变小
                            V.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)

                            UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 8, options: UIViewAnimationOptions.curveLinear, animations: {
                                V.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                                
                            })
                            
                        }
                    }
                }
                index = index + 1
            }
        }
    }
    
}
